home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / FWString / Sources / FWStrs.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  18.3 KB  |  520 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWStrs.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifdef FW_BUILD_MAC
  13. #include <Types.h>
  14. #endif
  15.  
  16. #ifndef   FWEXCLIB_H
  17. #include "FWExcLib.h"
  18. #endif
  19.  
  20. #ifndef FWMATH_H
  21. #include "FWMath.h"
  22. #endif
  23.  
  24. #ifndef FWSTRS_H
  25. #include "FWStrs.h"
  26. #endif
  27.  
  28. #ifndef FWPRIDEB_H
  29. #include "FWPriDeb.h"
  30. #endif
  31.  
  32. #ifndef FWSTRTOO_H
  33. #include "FWStrToo.h"
  34. #endif
  35.  
  36. #ifdef FW_BUILD_MAC
  37. #pragma segment Strings
  38. #endif
  39.  
  40. //========================================================================================
  41. //    CLASS FW_CString
  42. //========================================================================================
  43.  
  44. //----------------------------------------------------------------------------------------
  45. //    FW_CString::~FW_CString
  46. //----------------------------------------------------------------------------------------
  47.  
  48. FW_CString::~FW_CString()
  49. {
  50. }
  51.  
  52. //----------------------------------------------------------------------------------------
  53. //    FW_CString::FW_CString (Default constructor)
  54. //----------------------------------------------------------------------------------------
  55.  
  56. FW_CString::FW_CString() :
  57.     fRepresentation(0),
  58.     fLength(0),
  59. #ifdef FW_VARIABLE_WIDTH_CHARACTERS
  60.     fByteLength(0),
  61. #endif
  62.     fCapacity(0)
  63. {
  64. }
  65.  
  66. //----------------------------------------------------------------------------------------
  67. //    FW_CString::FW_CString (Copy constructor)
  68. //----------------------------------------------------------------------------------------
  69.  
  70. FW_CString::FW_CString(const FW_CString &string) :
  71.     fRepresentation(0),
  72.     fLength(0),            // Derived class is responsible for updating cached lengths
  73. #ifdef FW_VARIABLE_WIDTH_CHARACTERS
  74.     fByteLength(0),
  75. #endif
  76.     fCapacity(0)
  77. {
  78. }
  79.  
  80. //----------------------------------------------------------------------------------------
  81. //    FW_CString::Retrieve
  82. //----------------------------------------------------------------------------------------
  83.  
  84. void FW_CString::Retrieve(FW_Char* items, FW_CharacterCount numberItems, FW_CharacterPosition position) const
  85. {
  86.     const FW_Byte* rep = fRepresentation;
  87.     FW_ASSERT(numberItems >= 0);
  88.     FW_ASSERT(position>=0);
  89.     FW_ASSERT(position+numberItems <= fLength+1);    // allow retrieval of NUL terminator
  90.     const FW_Byte* start = rep + FW_BytesInString(rep, position);
  91.     FW_ByteCount bytes = FW_BytesInString(start, numberItems);
  92.     FW_BlockMove(start, (FW_Byte*) items, bytes);
  93. }
  94.  
  95. //----------------------------------------------------------------------------------------
  96. //    FW_CString::Insert
  97. //----------------------------------------------------------------------------------------
  98.  
  99. void FW_CString::Insert(const FW_Char* items, 
  100.                         FW_CharacterCount numberItems,  
  101.                         FW_CharacterPosition position)
  102. {
  103.     FW_ASSERT(numberItems >= 0);
  104.     FW_ASSERT(position>=0 && position<=fLength);
  105.     if (numberItems > 0)
  106.     {
  107.         FW_ByteCount bytesInItems = FW_BytesInString(items, numberItems);
  108.         FW_ByteCount capacityNeeded = GetByteLength()+bytesInItems;
  109.         GrowCapacity(capacityNeeded);
  110.         FW_ASSERT(fCapacity >= capacityNeeded);
  111.         
  112.         FW_ByteCount bytesToPosition = FW_BytesInString(fRepresentation, position);
  113.         FW_Byte* blockToMove = fRepresentation + bytesToPosition;
  114.         FW_BlockMove(blockToMove, 
  115.                      blockToMove+bytesInItems, 
  116.                      GetByteLength()-bytesToPosition+sizeof(FW_Char));
  117.         FW_BlockMove((const FW_Byte*) items, blockToMove, bytesInItems);
  118.         SetLength(fLength + numberItems, capacityNeeded);
  119.     }
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //    FW_CString::Delete
  124. //----------------------------------------------------------------------------------------
  125.  
  126. void FW_CString::Delete(FW_CharacterCount numberItems, FW_CharacterPosition position)
  127. {
  128.     FW_ASSERT(numberItems >= 0);
  129.     FW_ASSERT(position>=0 && position<=fLength);
  130.     FW_ASSERT(position+numberItems <= fLength);
  131.     if (numberItems > 0)
  132.     {
  133.         FW_Byte* atPosition = FW_BytePositionInString(fRepresentation, position);
  134.         FW_ByteCount toDelete = FW_BytesInString(atPosition, numberItems);
  135.         FW_ByteCount toMove   = FW_BytesInString(atPosition+toDelete, fLength-(position+numberItems));
  136.         FW_BlockMove(atPosition+toDelete, atPosition, toMove);
  137.         SetLength(fLength-numberItems, GetByteLength() - toDelete);
  138.     }
  139. }
  140.         
  141. //----------------------------------------------------------------------------------------
  142. //    FW_CString::Replace
  143. //----------------------------------------------------------------------------------------
  144.  
  145. void FW_CString::Replace(const FW_Char* items, FW_CharacterCount numberItems, FW_CharacterPosition position)
  146. {
  147.     FW_ASSERT(numberItems >= 0);
  148.     FW_ASSERT(position>=0 && position<=fLength);
  149.     if (numberItems > 0)
  150.     {
  151.         FW_ByteCount bytesToPosition = FW_BytesInString(fRepresentation, position);
  152.         FW_ByteCount bytesInItems = FW_BytesInString(items, numberItems);
  153.         FW_CharacterCount charsToReplace = FW_Minimum(numberItems, fLength-position);
  154.         FW_ByteCount bytesToReplace = FW_BytesInString(fRepresentation+bytesToPosition, charsToReplace);
  155.         
  156.         FW_ByteCount capacityNeeded = GetByteLength() - bytesToReplace + bytesInItems;
  157.         FW_ByteCount capacity = GrowCapacity(capacityNeeded);
  158.         FW_ASSERT(capacityNeeded <= capacity);
  159.         
  160.         FW_ByteCount bytesAtEndToMove = GetByteLength() - bytesToPosition - bytesToReplace;
  161.         if (bytesAtEndToMove>0 && bytesToReplace!=bytesInItems)
  162.         {
  163.             FW_BlockMove(fRepresentation+bytesToPosition+bytesToReplace,
  164.                          fRepresentation+bytesToPosition+bytesInItems,
  165.                          bytesAtEndToMove);
  166.         }
  167.         
  168.         FW_BlockMove((const FW_Byte*) items, fRepresentation+bytesToPosition, bytesInItems);
  169.         SetLength(fLength - charsToReplace + numberItems, capacityNeeded);
  170.     }
  171. }
  172.  
  173. //----------------------------------------------------------------------------------------
  174. //    FW_CString::ToUpper
  175. //----------------------------------------------------------------------------------------
  176.  
  177. void FW_CString::ToUpper()
  178. {
  179.     FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
  180.     tool->ToUpper(*this);
  181. }
  182.  
  183. //----------------------------------------------------------------------------------------
  184. //    FW_CString::ToLower
  185. //----------------------------------------------------------------------------------------
  186.  
  187. void FW_CString::ToLower()
  188. {
  189.     FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
  190.     tool->ToLower(*this);
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. //    FW_CString::Substitute
  195. //----------------------------------------------------------------------------------------
  196.  
  197. FW_Boolean FW_CString::Substitute(const FW_CString &searchString,
  198.                                         const FW_CString &substitutionString)
  199. {
  200.     FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
  201.     return tool->Substitute(*this, searchString, substitutionString);
  202. }
  203.  
  204. //----------------------------------------------------------------------------------------
  205. //    FW_CString::FindSubString
  206. //----------------------------------------------------------------------------------------
  207.  
  208. FW_Boolean FW_CString::FindSubString(const FW_CString &subString,
  209.                                                   FW_CharacterPosition &foundPosition,
  210.                                                  FW_CharacterPosition startPosition) const
  211. {
  212.     FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
  213.     return tool->FindSubString(*this, subString, foundPosition, startPosition);
  214. }
  215.  
  216. //----------------------------------------------------------------------------------------
  217. //    FW_CString::FindCharacter
  218. //----------------------------------------------------------------------------------------
  219.  
  220. FW_Boolean FW_CString::FindCharacter(FW_Char character,
  221.                                                  FW_CharacterPosition &foundPosition,
  222.                                                  FW_CharacterPosition startPosition) const
  223. {
  224.     FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
  225.     return tool->FindCharacter(*this, character, foundPosition, startPosition);
  226. }
  227.  
  228. //----------------------------------------------------------------------------------------
  229. //    FW_CString::FindWhiteSpace
  230. //----------------------------------------------------------------------------------------
  231.  
  232. FW_Boolean FW_CString::FindWhiteSpace(FW_CharacterPosition &foundPosition,
  233.                                                  FW_CharacterPosition startPosition) const
  234. {
  235.     FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
  236.     return tool->FindWhiteSpace(*this, foundPosition, startPosition);
  237. }
  238.  
  239. //----------------------------------------------------------------------------------------
  240. //    FW_CString::FindNonWhiteSpace
  241. //----------------------------------------------------------------------------------------
  242.  
  243. FW_Boolean FW_CString::FindNonWhiteSpace(FW_CharacterPosition &foundPosition,
  244.                                                  FW_CharacterPosition startPosition) const
  245. {
  246.     FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
  247.     return tool->FindNonWhiteSpace(*this, foundPosition, startPosition);
  248. }
  249.  
  250. //----------------------------------------------------------------------------------------
  251. //    FW_CString::Compare
  252. //----------------------------------------------------------------------------------------
  253.  
  254. int FW_CString::Compare(const FW_CString &string) const
  255. {
  256.     FW_CStringTool *tool = FW_CStringTool::GetCurrentStringTool();
  257.     return tool->CompareStrings(*this, string);
  258. }
  259.  
  260. //----------------------------------------------------------------------------------------
  261. //    FW_CString::operator==
  262. //----------------------------------------------------------------------------------------
  263.  
  264. FW_Boolean FW_CString::operator==(const FW_CString &string) const
  265. {
  266.     return (GetLength()==string.GetLength()) && (Compare(string) == kStringsEqual);
  267. }
  268.  
  269. //----------------------------------------------------------------------------------------
  270. //    FW_CString::operator!=
  271. //----------------------------------------------------------------------------------------
  272.  
  273. FW_Boolean FW_CString::operator!=(const FW_CString &string) const
  274. {
  275.     return (GetLength()!=string.GetLength()) || (Compare(string) != kStringsEqual);
  276. }
  277.  
  278. //----------------------------------------------------------------------------------------
  279. //    FW_CString::operator<
  280. //----------------------------------------------------------------------------------------
  281.  
  282. FW_Boolean FW_CString::operator<(const FW_CString &string) const
  283. {
  284.     return Compare(string) == kStringOneLess;
  285. }
  286.  
  287. //----------------------------------------------------------------------------------------
  288. //    FW_CString::operator>
  289. //----------------------------------------------------------------------------------------
  290.  
  291. FW_Boolean FW_CString::operator>(const FW_CString &string) const
  292. {
  293.     return Compare(string) == kStringOneGreater;
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    FW_CString::operator<=
  298. //----------------------------------------------------------------------------------------
  299.  
  300. FW_Boolean FW_CString::operator<=(const FW_CString &string) const
  301. {
  302.     return Compare(string) != kStringOneGreater;
  303. }
  304.  
  305. //----------------------------------------------------------------------------------------
  306. //    FW_CString::operator>=
  307. //----------------------------------------------------------------------------------------
  308.  
  309. FW_Boolean FW_CString::operator>=(const FW_CString &string) const
  310. {
  311.     return Compare(string) != kStringOneLess;
  312. }
  313.  
  314. //========================================================================================
  315. //    CLASS FW_CDynamicString
  316. //========================================================================================
  317.  
  318. //----------------------------------------------------------------------------------------
  319. //    FW_CDynamicString::~FW_CDynamicString
  320. //----------------------------------------------------------------------------------------
  321.  
  322. FW_CDynamicString::~FW_CDynamicString()
  323. {
  324.     FW_START_DESTRUCTOR
  325.     delete [] fRepresentation;
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. //    FW_CDynamicString::FW_CDynamicString
  330. //----------------------------------------------------------------------------------------
  331.  
  332. FW_CDynamicString::FW_CDynamicString(const FW_CDynamicString &string) :
  333.     FW_CString()
  334. {
  335.     FW_ByteCount numberBytes = string.GetByteLength();
  336.     AllocateRepresentation(numberBytes);
  337.     Append(string);
  338.     FW_END_CONSTRUCTOR
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------
  342. //    FW_CDynamicString::FW_CDynamicString
  343. //----------------------------------------------------------------------------------------
  344.  
  345. FW_CDynamicString::FW_CDynamicString(const FW_CString &string) :
  346.     FW_CString()
  347. {
  348.     FW_ByteCount numberBytes = string.GetByteLength();
  349.     AllocateRepresentation(numberBytes);
  350.     Append(string);
  351.     FW_END_CONSTRUCTOR
  352. }
  353.  
  354. //----------------------------------------------------------------------------------------
  355. //    FW_CDynamicString::FW_CDynamicString
  356. //----------------------------------------------------------------------------------------
  357.  
  358. FW_CDynamicString::FW_CDynamicString(const FW_Char *items, FW_CharacterCount numberItems) :
  359.     FW_CString()
  360. {
  361.     FW_ByteCount numberBytes = FW_BytesInString(items, numberItems);
  362.     AllocateRepresentation(numberBytes);
  363.     Append(items, numberItems);
  364.     FW_END_CONSTRUCTOR
  365. }
  366.  
  367. //----------------------------------------------------------------------------------------
  368. //    FW_CDynamicString::FW_CDynamicString
  369. //----------------------------------------------------------------------------------------
  370.  
  371. FW_CDynamicString::FW_CDynamicString(const FW_Char *items)
  372. {
  373.     FW_CharacterCount numberItems = FW_StringLength(items);
  374.     FW_ByteCount numberBytes = FW_BytesInString(items, numberItems);
  375.     AllocateRepresentation(numberBytes);
  376.     Append(items, numberItems);
  377.     FW_END_CONSTRUCTOR
  378. }
  379.  
  380. //----------------------------------------------------------------------------------------
  381. //    FW_CDynamicString::FW_CDynamicString
  382. //----------------------------------------------------------------------------------------
  383.  
  384. FW_CDynamicString::FW_CDynamicString(FW_ByteCount capacity) :
  385.     FW_CString()
  386. {
  387.     AllocateRepresentation(capacity);
  388.     FW_END_CONSTRUCTOR
  389. }
  390.  
  391. //----------------------------------------------------------------------------------------
  392. //    FW_CDynamicString::GrowCapacity
  393. //----------------------------------------------------------------------------------------
  394.  
  395. FW_ByteCount FW_CDynamicString::GrowCapacity(FW_ByteCount capacityNeeded)
  396. {
  397.     Resize(capacityNeeded);
  398.     return fCapacity;
  399. }
  400.  
  401. //----------------------------------------------------------------------------------------
  402. //    FW_CDynamicString::Resize
  403. //----------------------------------------------------------------------------------------
  404.  
  405. void FW_CDynamicString::Resize(FW_ByteCount newCapacity)
  406. {
  407.     if (fCapacity < newCapacity)
  408.     {
  409.         FW_Byte *newRep = new FW_Byte[newCapacity+sizeof(FW_Char)];
  410.         FW_BlockMove(fRepresentation, newRep, fCapacity+sizeof(FW_Char));
  411.         delete [] fRepresentation;
  412.         fRepresentation = newRep;
  413.         fCapacity = newCapacity;
  414.     }
  415. }
  416.  
  417. //========================================================================================
  418. //    CLASS FW_CStringReader
  419. //========================================================================================
  420.  
  421. //----------------------------------------------------------------------------------------
  422. //    FW_CStringReader::FW_CStringReader
  423. //----------------------------------------------------------------------------------------
  424.  
  425. FW_CStringReader::FW_CStringReader(
  426.                                         const FW_CString& string) :
  427.     FW_CTextReader(string.fRepresentation,
  428.                     string.fRepresentation+string.GetByteLength(),
  429.                     string.GetLength())
  430. {
  431. }
  432.  
  433. //----------------------------------------------------------------------------------------
  434. //    FW_CStringReader::FW_CStringReader
  435. //----------------------------------------------------------------------------------------
  436.  
  437. FW_CStringReader::FW_CStringReader(
  438.                                 const FW_CString &string,
  439.                                 FW_CharacterPosition start,
  440.                                 FW_CharacterCount   length) :
  441.     FW_CTextReader(FW_BytePositionInString(string.fRepresentation, start),
  442.                     FW_BytePositionInString(string.fRepresentation, start+length),
  443.                     length)
  444. {
  445. }
  446.  
  447. //----------------------------------------------------------------------------------------
  448. //    FW_CStringReader::DoGetNextBuffer
  449. //----------------------------------------------------------------------------------------
  450.  
  451. void FW_CStringReader::DoGetNextBuffer()
  452. {
  453.     FW_ASSERT(FALSE);    // one contigous block, there is no next buffer
  454. }
  455.  
  456. //----------------------------------------------------------------------------------------
  457. //    FW_CStringReader::DoGetPreviousBuffer
  458. //----------------------------------------------------------------------------------------
  459.  
  460. void FW_CStringReader::DoGetPreviousBuffer()
  461. {
  462.     FW_ASSERT(FALSE);    // one contigous block, there is no previous buffer
  463. }
  464.  
  465. //========================================================================================
  466. //    CLASS FW_CStringWriter
  467. //========================================================================================
  468.  
  469. //----------------------------------------------------------------------------------------
  470. //    FW_CStringWriter::FW_CStringWriter
  471. //----------------------------------------------------------------------------------------
  472.  
  473. FW_CStringWriter::FW_CStringWriter(
  474.                                                         FW_CString &string, 
  475.                                                         FW_TextWriterMode mode,
  476.                                                         unsigned short expansion) : 
  477.     FW_CTextWriter(0, 0), 
  478.     fString(string),
  479.     fExpansion(expansion)
  480. {
  481.     fBuffer = new FW_Byte[fExpansion];
  482.     fNext = fBuffer;
  483.     fLimit = fBuffer + fExpansion;
  484.     if (mode == FW_kTextAppend)
  485.     {
  486.         fBufferSum = fString.GetLength();
  487.     }
  488.     FW_END_CONSTRUCTOR
  489. }
  490.  
  491. //----------------------------------------------------------------------------------------
  492. //    FW_CStringWriter::~FW_CStringWriter
  493. //----------------------------------------------------------------------------------------
  494.  
  495. FW_CStringWriter::~FW_CStringWriter()
  496. {
  497.     FW_START_DESTRUCTOR
  498.     FlushAndUpdateText();
  499.     delete [] fBuffer;
  500. }
  501.  
  502. //----------------------------------------------------------------------------------------
  503. //    FW_CStringWriter::DoFlushAndGetNextBuffer
  504. //----------------------------------------------------------------------------------------
  505.  
  506. void FW_CStringWriter::DoFlushAndGetNextBuffer()
  507. {
  508.     fString.Replace((FW_Char*) fBuffer, FW_CharactersInBlock(fBuffer, fExpansion), fBufferSum);
  509.     fNext = fBuffer;
  510. }
  511.  
  512. //----------------------------------------------------------------------------------------
  513. //    FW_CStringWriter::FlushAndUpdateText
  514. //----------------------------------------------------------------------------------------
  515.  
  516. void FW_CStringWriter::FlushAndUpdateText()
  517. {
  518.     fString.Replace((FW_Char*) fBuffer, FW_CharactersInBlock(fBuffer, fNext-fBuffer), fBufferSum);
  519. }
  520.